import { Message, MessageBox, Loading } from 'element-ui'

const utils = {}

const msg = {} //消息类方法
const table = {} //表格类方法
const json = {} //json操作方法
const date = {} //日期操作方法

msg.info = (msg, fun) => {//消息
    Message({message: msg, duration: 1500, center: true, onClose: fun, showClose: true})
}
msg.success = (msg, fun) => {//成功
    Message({message: msg, duration: 1500, center: true, type: 'success', onClose: fun, showClose: true})
}
msg.warn = (msg, fun) => {//警告
    Message({message: msg, duration: 1500, center: true, type: 'warning', onClose: fun, showClose: true})
}
msg.error = (msg, fun) => {//错误
    Message.error({message: msg, duration: 1500, center: true, onClose: fun, showClose: true})
}
msg.alert = args => {
    MessageBox(JSON.stringify(args), '提示', {
        showClose: false,
    })
}
const loading = (options = {fullscreen: true, background: 'transparent'}) => {
    return  Loading.service(options)
}
const loadding = status => {
    status ? loading() : loading().close()
}

table.fullHeight = (reduceHeight) => {
    let clientHeight = document.body.clientHeight;
    let searchFormHeight = 0;
    let searchForm = document.querySelectorAll('.search-form-inline');
    if(searchForm.length > 0){
        searchFormHeight = document.querySelectorAll('.search-form-inline')[0].clientHeight
    }
    let h = clientHeight - searchFormHeight - 240 - (reduceHeight||0);
    return h<200?200:h;
}

json.removeItemFromArr = (arr, k, v) => {
    for(let i=0; i<arr.length; i++){
        if(v == arr[i][k]){
            arr.splice(i,1);
            return;
        }
    }
}

date.toDate = value => {
    if(!value) return '';
    let date;
    if(typeof value == 'number'){
        date = new Date(value);
    } else if (typeof value == 'object'){
        date = value;
    } else {
        return value;
    }
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    return y + '-' + MM + '-' + d;
}
date.toDatetime = value => {
    if(!value) return '';
    let date;
    if(typeof value == 'number'){
        date = new Date(value);
    } else if (typeof value == 'object'){
        date = value;
    } else {
        return value;
    }
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    let m = date.getMinutes();
    m = m < 10 ? ('0' + m) : m;
    let s = date.getSeconds();
    s = s < 10 ? ('0' + s) : s;
    return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
}

utils.msg = msg;
utils.table = table;
utils.json = json;
utils.date = date;
utils.$loading = loadding

export default utils
